package aControllerLayer;

import java.util.Timer;
import java.util.TimerTask;

import aModelLayer.Game;
import aModelLayer.Load;
import aModelLayer.Menu;
import aViewLayer.TitleView;
import aViewLayer.View;
import aaGame.GameEngine;
import azServiceLayer.AudioPlayer;
import azServiceLayer.Input;
import azServiceLayer.R;

public class TitleMenuController extends Controller{
	Menu mMenu;
	GameEngine mEngine;
	Game mModel;
	private TitleView mTitleView;
	AudioPlayer player;
	
	public TitleMenuController(Menu menu, Game model, GameEngine engine){
		player = new AudioPlayer(this.getClass().getResource(R.music.finalfantasy2), AudioPlayer.MIDI);
		mModel = model;
		mMenu = menu;
		mEngine = engine;
		mTitleView = new TitleView(menu);
	}
	
	public void keyPressed(int key) {
		// TODO Auto-generated method stub
		
		switch(key){
		
		case Input.DOWN: 
			mMenu.nextOption();
			break;
		case Input.UP:
			mMenu.previousOption();
			break;
		case Input.ENTER:
			switch(mMenu.getCurrentOption()){
			case "Start New Game":
				player.stopMusic();
				Timer timer;
				TimerTask task = new TimerTask() {
					// Delays the loading of a room (allowing the view to fade in and out)
					@Override
					public void run() {
						mEngine.startNewGame();
						mEngine.printControllerName();
					}
				};
				timer = new Timer();
				timer.schedule(task, 600);
				break;
			case "Load Game":
				player.stopMusic();
				Timer timer1;
				TimerTask task1 = new TimerTask() {
					// Delays the loading of a room (allowing the view to fade in and out)
					@Override
					public void run() {
						mEngine.loadGame();
					}
				};
				timer1 = new Timer();
				timer1.schedule(task1, 800);
				;
				break;
			case "Exit":
				Timer timer11;
				TimerTask task11 = new TimerTask() {
					// Delays the loading of a room (allowing the view to fade in and out)
					@Override
					public void run() {
						System.exit(0);
					}
				};
				timer11 = new Timer();
				timer11.schedule(task11, 800);
				System.out.println("Exiting Game");
				break;
			}
			mMenu.select();
			break;
		}
	}

	public void keyReleased(int key) {
		// TODO Auto-generated method stub
	}

	public void update() {
		// TODO Auto-generated method stub
	}
	
	public void p(String s){
		System.out.print("\t"+s+",");
	}
	
	public void pl(String s){
		System.out.println("");
	}

	public void playMusic(){
		player.playMusic();
	}

	@Override
	public View getView() {
		// TODO Auto-generated method stub
		return mTitleView;
	}
}
